#include "player.hpp"

#include "scence.hpp"

IMPL_LOGGER(Scence, logger);

Scence::Scence(Connection & conn, const SSReportAreaReq & req)
{
	_conn           = &conn;

	_svr_id         = req._svr_id;
	_area_id        = req._area_id;
	_online         = 0;
	_room_capacity  = req._room_capacity;

	_room_map.clear();
	
	std::vector<ROOMID>::const_iterator citer = req._room_list.begin();
	for (; citer != req._room_list.end(); ++ citer)
	{
		ROOMID room_id = *citer;
		RoomB roomb;
		roomb._id = room_id;
		if (!_room_map.insert(std::make_pair(room_id, roomb)).second)
		{
			LOG4CPLUS_ERROR(logger, "duplicate room id. areaid: " << (int)_area_id 
							<< ", roomid: " << (int)room_id);
		}
	}
}

Scence::~Scence()
{
	_room_map.clear();
}

bool Scence::reserve_seat(ROOMID room_id)
{
	RoomMap_Iter iter = _room_map.find(room_id);

	if (_room_map.end() == iter)
	{
		LOG4CPLUS_ERROR(logger, "reserve seat error. not found the room. id: " << (int)room_id);
		
		return false;
	}

	if (iter->second._online >= _room_capacity)
	{
		return false;
	}

	++ iter->second._online;

	return true;
}

void Scence::cancel_seat(ROOMID room_id)
{
	RoomMap_Iter iter = _room_map.find(room_id);

	if (_room_map.end() == iter)
	{
		LOG4CPLUS_ERROR(logger, "cancel seat error. not found the room. id: " << (int)room_id);
		
		return ;
	}

	-- iter->second._online;
}

int Scence::fill_room_b(std::vector<RoomB> & room_list)
{
	RoomMap_Iter iter = _room_map.begin();
	for (; iter != _room_map.end(); ++ iter)
	{
		room_list.push_back(iter->second);
	}

	return _room_map.size();
}

